package com.xnccc.fragment;

import com.owen.mapx.MapX;
import com.xnccc.audioset.MainActivity;
import com.xnccc.audioset.R;
import com.xnccc.view.VerticalSeekBar;
import com.xnccc.view.VerticalSeekBar.OnSeekBarChangeListener;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import util.DP;

public class EqFragment extends Fragment implements OnSeekBarChangeListener, OnClickListener {

	private MapX bird;
	private int nowDD = MapX.CH_L;

	int show[] = { R.id.txtshow1, R.id.txtshow2, R.id.txtshow3, R.id.txtshow4, R.id.txtshow5, R.id.txtshow6,
			R.id.txtshow7, R.id.txtshow8, R.id.txtshow9, R.id.txtshow10 };
	int bars[] = { R.id.seekbar1, R.id.seekbar2, R.id.seekbar3, R.id.seekbar4, R.id.seekbar5, R.id.seekbar6,
			R.id.seekbar7, R.id.seekbar8, R.id.seekbar9, R.id.seekbar10 };
	TextView[] showTxts = new TextView[10];
	VerticalSeekBar seekBars[] = new VerticalSeekBar[10];
	CheckBox noiseSwitch;
	RadioGroup rg;
	private TextView tvText;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		bird = MainActivity.bird;
		View mView = inflater.inflate(R.layout.fragment_eq, container, false);
		initView(mView);
		return mView;
	}

	private void initBar(int i) {
		int nowpro = bird.getPEQ(nowDD, (i+1));
		showTxts[i].setText(nowpro + "");
		seekBars[i].setProgress(30 - nowpro);
	}

	private void initView(View mView) {
		tvText = (TextView) mView.findViewById(R.id.txtBar1);
		noiseSwitch = (CheckBox) mView.findViewById(R.id.noise_switch);
		for (int i = 0; i < bars.length; i++) {
			showTxts[i] = (TextView) mView.findViewById(show[i]);
			seekBars[i] = (VerticalSeekBar) mView.findViewById(bars[i]);
			seekBars[i].setOnSeekBarChangeListener(this);
			initBar(i);
		}
		RadioButton btn = (RadioButton) mView.findViewById(R.id.rb1);
		btn.setSelected(true);
		btn.setOnClickListener(this);
		mView.findViewById(R.id.rb1).setOnClickListener(this);
		mView.findViewById(R.id.rb2).setOnClickListener(this);
		mView.findViewById(R.id.rb3).setOnClickListener(this);
		mView.findViewById(R.id.rb4).setOnClickListener(this);
		mView.findViewById(R.id.rb5).setOnClickListener(this);
		rg = (RadioGroup) mView.findViewById(R.id.radiogroup);
		rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup arg0, int arg1) {

				switch (arg1) {
				case R.id.rb3:
					tvText.setText(DP.center+"HZ");
					break;
				}
				if (noiseSwitch.isChecked()) {
					switch (arg1) {
					case R.id.rb1:
						bird.setNoiseChannel(MapX.CH_L);
						break;
					case R.id.rb2:
						bird.setNoiseChannel(MapX.CH_R);
						break;
					case R.id.rb3:
						bird.setNoiseChannel(MapX.CH_C);
						break;
					case R.id.rb4:
						bird.setNoiseChannel(MapX.CH_SR);
						break;
					case R.id.rb5:
						bird.setNoiseChannel(MapX.CH_SL);
						break;
					}
				}
			}
		});
		noiseSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				if (arg1) {
					bird.switchSRC(MapX.SRC_NOISE);
					switch (rg.getCheckedRadioButtonId()) {
					case R.id.rb1:
						bird.setNoiseChannel(MapX.CH_L);
						break;
					case R.id.rb2:
						bird.setNoiseChannel(MapX.CH_R);
						break;
					case R.id.rb3:
						bird.setNoiseChannel(MapX.CH_C);
						break;
					case R.id.rb4:
						bird.setNoiseChannel(MapX.CH_SR);
						break;
					case R.id.rb5:
						bird.setNoiseChannel(MapX.CH_SL);
						break;

					default:
						break;
					}
				} else {
					bird.switchSRC(MapX.SRC_ARM);
					bird.setNoiseChannel(MapX.CH_NULL);
				}
			}
		});
	}

	private void chanage(int num, int vol) {
		showTxts[num].setText(vol + "");
		bird.setPEQ(nowDD, (num+1) , vol);
	}

	@Override
	public void onProgressChanged(VerticalSeekBar VerticalBar, int progress, boolean fromUser) {
		if (fromUser) {
			Log.e("now", progress+"");
			int nowindex = 30 - progress;
			switch (VerticalBar.getId()) {
			case R.id.seekbar1:
				chanage(0, nowindex);
				break;
			case R.id.seekbar2:
				chanage(1, nowindex);
				break;
			case R.id.seekbar3:
				chanage(2, nowindex);
				break;
			case R.id.seekbar4:
				chanage(3, nowindex);
				break;
			case R.id.seekbar5:
				chanage(4, nowindex);
				break;
			case R.id.seekbar6:
				chanage(5, nowindex);
				break;
			case R.id.seekbar7:
				chanage(6, nowindex);
				break;
			case R.id.seekbar8:
				chanage(7, nowindex);
				break;
			case R.id.seekbar9:
				chanage(8, nowindex);
				break;
			case R.id.seekbar10:
				chanage(9, nowindex);
				break;
			}
		}
	}

	@Override
	public void onStartTrackingTouch(VerticalSeekBar VerticalBar) {

	}

	@Override
	public void onStopTrackingTouch(VerticalSeekBar VerticalBar) {

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.rb1:
			nowDD = MapX.CH_L;
			break;
		case R.id.rb2:
			nowDD = MapX.CH_R;
			break;
		case R.id.rb3:
			nowDD = MapX.CH_C;
			break;
		case R.id.rb4:
			nowDD = MapX.CH_SR;
			break;
		case R.id.rb5:
			nowDD = MapX.CH_SL;
			break;
		}
		for (int i = 0; i < 10; i++) {
			initBar(i);
		}
	}
}
